import Vue from "vue";
import ElementUI from "element-ui"; // 引入 element 组件
import "element-ui/lib/theme-chalk/index.css"; // 加上 element 样式
import VueRouter from "vue-router";
import App from "./App.vue";

//Admin
import AHome from "pages/Ahome/AHome.vue";

//import RProduct from "@/pages/Ahome/product/classroom/RProduct.vue";
import RProductList from "@/pages/Ahome/product/classroom/RProductList.vue";
import RProductEdit from "@/pages/Ahome/product/classroom/RProductEdit.vue";

//import CProduct from "@/pages/Ahome/product/course/CProduct.vue";
import CProductList from "@/pages/Ahome/product/course/CProductList.vue";
import CProductEdit from "@/pages/Ahome/product/course/CProductEdit.vue";

//import SProduct from "@/pages/Ahome/product/student/SProduct.vue";
import SProductList from "@/pages/Ahome/product/student/SProductList.vue";
import SProductEdit from "@/pages/Ahome/product/student/SProductEdit.vue";

//import TProduct from "@/pages/Ahome/product/teacher/TProduct.vue";
import TProductList from "@/pages/Ahome/product/teacher/TProductList.vue";
import TProductEdit from "@/pages/Ahome/product/teacher/TProductEdit.vue";


//Teacher
import THome from "@/pages/Thome/THome.vue";
import Service from "@/pages/Thome/product/Service.vue";
import Check from "@/pages/Thome/product/Check.vue";
import Product from "@/pages/Thome/product/Product.vue";
import ProductList from "@/pages/Thome/product/ProductList.vue";
import ProductEdit from "@/pages/Thome/product/ProductEdit.vue";
import ServiceEdit from "@/pages/Thome/product/ServiceEdit.vue";


import Login from "@/pages/Login.vue";
import Register from "@/pages/register.vue";

//import {getGlobalData} from "utils/globalData";
import './mock'
import axios from 'axios'
Vue.prototype.axios = axios;

Vue.use(ElementUI); // 在 Vue 里使用 Element

Vue.use(VueRouter); // 使用 vue-router

/**
 * 路由关系：
 * /login                     /home                      
 * +------------------+       +-----------------+     
 * | App              |       | App             |     
 * | +--------------+ |       | +-------------+ |      
 * | | Login        | |       | | Home        | |     
 * | |              | |       | |             | |       
 * | |              | |  +--) | |<router-view>| |  
 * | |              | |       | |  无对应内容  | |       
 * | |              | |       | |             | |     
 * | +--------------+ |       | +-------------+ |     
 * +------------------+       +-----------------+       
 * 
 *       /home/product                /home/product/list              /home/product/edit          
 *       +---------------------+      +------------------------+      +------------------------+
 *       | App                 |      | App                    |      | App                    |
 *       | +-----------------+ |      | +--------------------+ |      | +--------------------+ |
 *       | | Home            | |      | | Home               | |      | | Home               | |
 *       | | +-------------+ | |      | | +----------------+ | |      | | +----------------+ | |
 *  +--) | | | Product     | | | +--) | | | Product        | | | +--) | | | Product        | | |
 *       | | |<router-view>| | |      | | | +------------+ | | |      | | | +------------+ | | |          
 *       | | |  无对应内容  | | |      | | | | ProductList| | | |      | | | | ProductEdit| | | |          
 *       | | |             | | |      | | | | 单列表页    | | | |      | | | | 单表单页    | | | |          
 *       | | |             | | |      | | | +------------+ | | |      | | | +------------+ | | |          
 *       | | +-------------+ | |      | | +----------------+ | |      | | +----------------+ | |
 *       | +-----------------+ |      | +--------------------+ |      | +--------------------+ |
 *       +---------------------+      +------------------------+      +------------------------+
 */

// 配置路由信息
const routes = [{
  path: "/", // 父路由路径
  component: App, // 父路由组件，传入 vue component
  redirect: '/login',
  name: "App", // 路由名称
  // 设置子路由
  children: [
    {
      path: "login", // 子路由路径
      component: Login, // 子路由组件，会替换父组件中<router-view>中的内容
      name: "Login" // 路由名称
    },

    {
      path: "register", // 子路由路径
      component: Register, // 子路由组件，会替换父组件中<router-view>中的内容
      name: "Register" // 路由名称
    },


    //管理员界面
    {
      // 应用首页
      path: "Ahome",
      component: AHome,
      name: 'AHome',
      children: [
        // 教室管理
        // { 
        //   path: "Classroom", component: RProduct, name: 'Classroom', meta:{title:"教室管理", icon:'example'},
        //   children: [ // 子路由内容

        {
          path: "rlist",
          component: RProductList,
          name: 'Rlist'
        },

        {
          path: "radd/0",
          component: RProductEdit,
          name: "Radd"
        },

        {
          path: "redit/:crId",
          component: RProductEdit,
          name: "Redit"
        },
        // ]},
        // 课程管理
        // { path: "Course", component: CProduct, name: "Course", meta:{title:"课程管理", icon:'example'},
        //   children: [ // 子路由内容

        {
          path: "clist",
          component: CProductList,
          name: "Clist"
        },

        {
          path: "cadd/0",
          component: CProductEdit,
          name: "Cadd"
        },

        {
          path: "cedit/:courseId",
          component: CProductEdit,
          name: "Cedit"
        },
        //   ]
        // },
        //学生管理
        // {
        //   path: "Student", component: SProduct, name: "Student", meta:{title:"学生管理", icon:'example'},
        //   children: [ // 子路由内容

        {
          path: "slist",
          component: SProductList,
          name: "Slist"
        },

        {
          path: "sadd/0",
          component: SProductEdit,
          name: "Sadd"
        },

        {
          path: "sedit/:sId",
          component: SProductEdit,
          name: "Sedit"
        },
        //   ]
        // },
        //教师管理
        // {
        //   path: "Teacher", component: TProduct, name: "Teacher", meta:{title:"教师管理", icon:'example'},
        //   children: [ // 子路由内容

        {
          path: "tlist",
          component: TProductList,
          name: "Tlist"
        },

        {
          path: "tadd/0",
          component: TProductEdit,
          name: "Tadd"
        },
        {
          path: "twadd/0",
          component: ServiceEdit,
          name: "Twadd"
        },

        {
          path: "tedit/:tid",
          component: TProductEdit,
          name: "Tedit"
        }
        //   ]
        // }
      ]
    },

    //教师界面
    {
      // 应用首页
      path: "Thome",
      component: THome,
      name: "THome",
      children: [
        // 服务列表
        {
          path: "Service",
          component: Service,
          name: "Service",
          meta: {
            title: "学生管理",
            icon: 'example'
          }
        },
        {
          path: "Check",
          component: Check,
          name: "Check",
          meta: {
            title: "考勤查询",
            icon: 'example'
          }
        },
        // 产品容器
        {
          path: "Product",
          component: Product,
          name: "Product",
          meta: {
            title: "学生管理",
            icon: 'example'
          },
          children: [ // 子路由内容
            // 产品列表
            {
              path: "list",
              component: ProductList,
              name: "ProductList"
            },
            // 产品新增
            {
              path: "add/0",
              component: ProductEdit,
              name: "ProductAdd"
            },
            {
              path: "add/0",
              component: ServiceEdit,
              name: "ServiceAdd"
            },
            // 产品编辑
            // 我们能看到，新增和编辑其实最终使用的是同一个组件
            // 当 edit/:id 匹配成功，
            // ProductEdit 会被渲染在 Product 的 <router-view> 中
            {
              path: "edit/:id",
              component: ProductEdit,
              name: "ProductEdit"
            },
            {
              path: "edit/:id",
              component: ServiceEdit,
              name: "ServiceEdit"
            },
            {
              path: "check",
              component: Check,
              name: "Check"
            }
          ]
        }
      ]
    }
  ]
}];

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
  routes // （缩写）相当于 routes: routes
});

router.beforeEach((to, from, next) => {
  // if (to.name !== "Login") {
  //   // 非 login 页面，检查是否登录
  //   // 这里简单前端模拟是否填写了用户名，真实环境需要走后台登录，缓存到本地
  //   if (!getGlobalData("username")) {
  //     next({
  //       name: "Login"
  //     });
  //   }
  // }
  // 其他情况正常执行
  next();
});


// 启动一个 Vue 应用
new Vue({
  el: "#app",
  router, // 传入路由能力
  render: h => h(App)
});